package com.ghost.alibabaghost.web.controller.sys;

import com.ghost.alibabaghost.api.model.sys.GhostDictModel;
import com.ghost.alibabaghost.api.service.sys.GhostDictService;
import com.ghost.alibabaghost.result.SingleResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author yaolisheng
 * @version 1.0
 * @date 2022/1/24 06:50
 */

@RestController
@RequestMapping("dict")
public class GhostDictController {
    private static final Logger logger = LoggerFactory.getLogger(GhostDictController.class);

    private static final String DICT_PARENT_CODE = "parentCode";

    @Autowired
    private GhostDictService dictService;

    /**
     * 根据父级编码查询列表tree
     *
     * @param
     * @return
     */
    @RequestMapping("queryByTree")
    public SingleResult<List<GhostDictModel>> queryByTree() {
        logger.info("queryByParentCode(获取字典列表tree)");
        try {
            List<GhostDictModel> ghostDictModels = dictService.getTreeList(DICT_PARENT_CODE);

            logger.info("queryByParentCode(获取字典列表)-->respose:" + ghostDictModels);
            return SingleResult.buildSuccess(ghostDictModels);
        } catch (Exception e) {
            logger.error("queryByParentCode(获取字典列表):error-->", e);
            return SingleResult.buildFailure();
        }
    }

    /**
     * 根据父级编码查询列表
     * @param parentCode
     * @return
     */
    @RequestMapping("queryByParentCode")
    public SingleResult<List<GhostDictModel>> queryByParentCode(@RequestParam("parentCode") String parentCode) {
        logger.info("queryByParentCode(获取字典列表)-->param:" + parentCode);
        try {
            List<GhostDictModel> ghostDictModels = dictService.queryByParentCode(parentCode);
            logger.info("queryByParentCode(获取字典列表)-->respose:" + ghostDictModels);
            return SingleResult.buildSuccess(ghostDictModels);
        } catch (Exception e) {
            logger.error("queryByParentCode(获取字典列表)--", e);
            return SingleResult.buildFailure();
        }
    }

    /**
     * 插入
     * @param ghostDictModel
     * @return
     */
    @RequestMapping("insertDict")
    public SingleResult<String> insertDict(@RequestParam GhostDictModel ghostDictModel) {
        logger.info("insertDict新增--->param:" + ghostDictModel);
        try {
            dictService.insertDict(ghostDictModel);
            return SingleResult.buildSuccess("成功");
        } catch (Exception e) {
            logger.error("insertDict插入失败", e);
            return SingleResult.buildFailure();
        }
    }

    @RequestMapping("deleteById")
    public SingleResult<String> deleteById(@RequestParam("id") Integer id){
        logger.info("deleteById根据主键删除--->param："+id);
        try {
            dictService.deleteByDictCode(id);
            return SingleResult.buildSuccess("成功");
        }catch (Exception e){
            logger.error("deleteById删除失败",e);
            return SingleResult.buildFailure();
        }
    }

}
